<?php
include_once("../inc/constants.inc.php");
include_once("../inc/connection.php");
session_start();
unset($_COOKIE['timer']);
setcookie("timer", "", time()-3600);
// define variables and set to empty values

if ($_SERVER["REQUEST_METHOD"] == "POST")
{		
	echo "</pre>";
	//print_r($_POST);
	echo "</pre>";
	$test = $_POST;
	$test_type = $_POST['test_type'];
	$studentId = $_SESSION['userID'];
	unset($test['email']);
	unset($test['submit']);
	unset($test['duration']);
	unset($test['test_type']);	
	foreach($test as $value)
	{
		$data = explode("_", $value);
		if(1){ // Add validation here
		
			$answerQuery = "select * from question_table where questionNumber=".$data['1'];
			echo $answerQuery;
			$res = mysql_query($answerQuery);
			$row = mysql_fetch_array($res);
			if(count($row) > 0)
			{
				$correctAnswer = $row['correctAnswer'];
				$correctOption = $row['correctOption'];
				if($row['correctOption'] == $data[2])
					$result = "correct";
				else
					$result = "wrong";
			}else
			$result = "";

			//echo "</br>studentid= $studentId, question no = $data[0], questionId = $data[1], answer=$data[2], result=$result, test_type=$test_type";
			$sql = "insert into `answer_sheet` (`StudentID`, `QuesNO`, `QuesID`, `Answer`, `Result`, `typeId`, `isReset`) values ('$studentId','$data[0]','$data[1]','$data[2]','$result','$test_type', 'no')";
			echo $sql;
			if(!mysql_query($sql))
			echo "error occurred";
		}
	}
		$q = "UPDATE `login_details` SET submitted='yes' WHERE userID=$studentId";
		if(!mysql_query($q))
			echo "error occurred";
		header("Location: ../test.php?test=2");

	
	// Validation goes here
	/*
	if($name!= "")
	{
		RegistrationErr = 1; 
	}
	*/
	
	// Check for duplicate email
	
	

}else
	 {
		header("Location: ../index.php?invalidAccess=1");
}
 
 
function fiter_input($data)
{
     $data = trim(mysql_real_escape_string($data));
     $data = htmlspecialchars($data);
     return $data;
}
?>